package edu.byu.intex2.data.bo;

import edu.byu.intex2.data.DataException;
import edu.byu.intex2.data.dao.PartDAO;
import edu.byu.intex2.data.dao.OrderDAO;
import edu.byu.intex2.data.dao.OrderLineDAO;

/**
 * Tracks each part on order
 * @author Brady
 */
public class OrderLine extends BusinessObject {
    // Quantity
    private int quantity;

    // Order
    private String orderId;
    private Order order;

    // Part
    private String partId;
    private Part part;

    /** Creates a new instance of BusinessObject */
    public OrderLine(String id) {
        super(id);
    }//constructor

    /** Convenience method to call the correct DAO to save this business object */
    public void save() throws DataException {
        try {
            OrderLineDAO.getInstance().save(this);
        } catch (DataException e) {
            e.printStackTrace();
        }
    }

    /** @return the quantity     */
    public double getQuantity() {
        return quantity;
    }

    /** @param actualPrice the actualPrice to set     */
    public void setQuantity(int quantity) {
        this.quantity = quantity;
        setDirty();
    }

    /** @return the orderId     */
    public String getOrderId() {
        return orderId;
    }

    /** @param orderId the orderId to set     */
    public void setOrderId(String orderId) {
        this.orderId = orderId;
        setDirty();
    }

    /** @return the order     */
    public Order getOrder() throws Exception {
        if (order == null) {
            order = OrderDAO.getInstance().read(orderId);
        }
        return order;
    }

    /** @param order the order to set     */
    public void setOrder(Order order) {
        this.order = order;
        setDirty();
    }

    /** @return the partId     */
    public String getPartId() {
        if (partId == null){
            partId = part.getId();
        }
        return partId;
    }

    /** @param partId the partId to set     */
    public void setPartId(String partId) {
        this.partId = partId;
        setDirty();
    }

    /** @return the part     */
    public Part getPart() throws Exception {
        if (part == null) {
            part = PartDAO.getInstance().read(partId);
        }
        return part;
    }

    /** @param part the part to set     */
    public void setPart(Part part) {
        this.part = part;
        setDirty();
    }
}
